package com.cjc.spinerpopwindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 * 下拉列表框
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>Coffee</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2015-3-20 下午4:27:57</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>SpinerPopWindow</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class MainActivity extends Activity implements OnClickListener {
	private TextView tv_value;
	private List<Map<String, Object>> nameList = new ArrayList<Map<String, Object>>();
	private SpinerPopWindow mSpinerPopWindow;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;
		tv_value = (TextView) findViewById(R.id.tv_value1);
		RelativeLayout ll_lowwarn1 = (RelativeLayout) findViewById(R.id.ll_lowwarn1);
		ll_lowwarn1.setOnClickListener(this);
		/* 初始化数据 */
		getDateData();
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 初始化数据
	 ***************************************************** 
	 */
	private void getDateData() {
		nameList.clear();
		nameList = new ArrayList<Map<String, Object>>();
		String date[] = getResources().getStringArray(R.array.date_array);
		for (int i = 0; i < date.length; i++) {
			Map<String, Object> new_obj = new HashMap<String, Object>();
			new_obj.put("text", date[i]);
			if (i == 0) {
				new_obj.put("img", R.drawable.android_checked_icon);
			} else {
				new_obj.put("img", R.drawable.android_check_icon);
			}
			nameList.add(new_obj);
		}
		// 初始化数据
		if (nameList.size() > 0) {
			if (!nameList.get(0).get("text").toString().equals(date[0])) {
				nameList.get(0).put("img", R.drawable.android_checked_icon);
			}
			tv_value.setText(nameList.get(0).get("text").toString());
		}
		mSpinerPopWindow = new SpinerPopWindow(context);
		mSpinerPopWindow.setAdatper(context, nameList);
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 给文本框赋值
	 * 
	 * @param pos
	 ***************************************************** 
	 */
	private void setHero(int pos) {
		if (pos >= 0 && pos <= nameList.size()) {
			String value = nameList.get(pos).get("text").toString();
			tv_value.setText(value.toString());
		}
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 显示PopWindow
	 ***************************************************** 
	 */
	private void showSpinWindow() {
		mSpinerPopWindow.setWidth(tv_value.getWidth());
		mSpinerPopWindow.showAsDropDown(tv_value);
	}

	/**
	 * 单击事件处理
	 */
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.ll_lowwarn1:
			/* 显示PopWindow */
			showSpinWindow();
			break;
		}
	}

	/**
	 * 
	 ***************************************************** 
	 * <hr>
	 * <dt><span class="strong">类功能简介:</span></dt>
	 * <dd>Coffee</dd>
	 * <dt><span class="strong">创建时间:</span></dt>
	 * <dd>2015-3-20 下午4:31:22</dd>
	 * <dt><span class="strong">公司:</span></dt>
	 * <dd>自定义SpinerPopWindow</dd>
	 * 
	 * @author aa1000777 - Email:aa1000777@qq.com
	 ***************************************************** 
	 */
	class SpinerPopWindow extends PopupWindow {
		private int balanceIndex = 0;
		private Context mContext;
		private ListView mListView;
		private SimpleAdapter mAdapter;
		private List<Map<String, Object>> list;

		public SpinerPopWindow(Context context) {
			super(context);
			mContext = context;
			init();
		}

		public void setAdatper(Context context, List<Map<String, Object>> data) {
			list = data;
			mAdapter = new SimpleAdapter(context, data, R.layout.activity_good, new String[] { "text", "img" },
					new int[] { R.id.list_text, R.id.list_radioImg });
			mListView.setAdapter(mAdapter);
		}

		private void init() {
			View view = LayoutInflater.from(mContext).inflate(R.layout.spiner_window_layout, null);
			setContentView(view);
			setWidth(LayoutParams.WRAP_CONTENT);
			setHeight(LayoutParams.WRAP_CONTENT);
			setFocusable(true);
			ColorDrawable dw = new ColorDrawable(0x00);
			setBackgroundDrawable(dw);
			mListView = (ListView) view.findViewById(R.id.listview);
			mListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					ChangeRadioImg(balanceIndex, false);
					ChangeRadioImg(position, true);
					balanceIndex = position;
				}
			});
		}

		/**
		 * 
		 ***************************************************** 
		 * 方法简介: item 事件处理
		 * 
		 * @param selectedItem
		 * @param b
		 ***************************************************** 
		 */
		private void ChangeRadioImg(int selectedItem, boolean b) {
			dismiss();
			SimpleAdapter la = mAdapter;
			HashMap<String, Object> map = (HashMap<String, Object>) la.getItem(selectedItem);
			if (b) {
				map.put("img", R.drawable.android_checked_icon);
			} else {
				map.put("img", R.drawable.android_check_icon);
			}
			la.notifyDataSetChanged();
			/* 给文本框赋值 */
			setHero(selectedItem);
		}
	}
}
